﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
//using WordField = Microsoft.Office.Interop.Word.Field;


namespace Thesis_VSTO
{
    
    public partial class Ribbon
    {
        Word.Application wordApp;

        Word.ApplicationEvents4_WindowBeforeDoubleClickEventHandler p;

        string bodyStyle;
        string figEnvStyle;
        string figCapStyle;
        string tabCapStyle;
        string eqnEvnStyle;
        string[] headingStyle;

        private void Ribbon_Load(object sender, RibbonUIEventArgs e)
        {
            wordApp = Globals.ThisAddIn.Application;


            bodyStyle = "论文正文";
            figEnvStyle = "图环境";
            figCapStyle = "图题";
            tabCapStyle = "表题";
            eqnEvnStyle = "公式环境";
            headingStyle = new string[4]{"1级标题", "2级标题", "3级标题", "4级标题"};
        }

        private void whutSetStyle(string styleName)
        {
            try
            {
                wordApp.Selection.Range.set_Style(styleName);
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                MessageBox.Show("样式“" + styleName + "”不存在！");
            }
            //wordApp.Selection.Range.set_Style(styleName);
        }

        private Word.Field placeField(string fieldCode)
        {
            wordApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart);
            return wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldEmpty, fieldCode, false);
        }

        private Word.Field placeShowField(string fieldCode)
        {
            wordApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart);
            Word.Field f = wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldEmpty, fieldCode, false);
            f.ShowCodes = true;
            return f;
        }

        private Word.Field placeUpdateField(string fieldCode)
        {
            wordApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart);
            Word.Field f = wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldEmpty, fieldCode, false);
            f.Update();
            return f;
        }

        private void insertCap(string fieldCode, string headText, string endText)
        {
            wordApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart);
            wordApp.Selection.TypeText(headText);

            wordApp.Selection.TypeText(endText);
        }


        private void btnHeading1_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(headingStyle[0]);
        }

        private void btnHeading2_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(headingStyle[1]);
        }

        private void btnHeading3_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(headingStyle[2]);
        }

        private void btnBody_Click(object sender, RibbonControlEventArgs e)
        {
            if (wordApp.Selection.get_Style().nameLocal != "论文正文")
            {
                whutSetStyle(bodyStyle);
            }
            //whutSetStyle(bodyStyle);
        }

        private void btnFigEnv_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(figEnvStyle);
        }

        private void btnFigCap_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(figCapStyle);
        }

        private void btnTabCap_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(tabCapStyle);
        }

        private void btnInsertFig_Click(object sender, RibbonControlEventArgs e)
        {
            whutSetStyle(figEnvStyle);
            wordApp.Selection.TypeParagraph();
            if (wordApp.Selection.get_Style().NameLocal != figCapStyle)
            {
                whutSetStyle(figCapStyle);
            }

            placeField("SEQ WHUTFig \\n \\* Arabic \\* MERGEFORMAT");
            //Word.Style s = wordApp.Selection.get_Style();
            //MessageBox.Show(s.NameLocal);

            if (p != null)
            {
                wordApp.WindowBeforeDoubleClick -= p;
            }
        }

        private void btnInsertTab_Click(object sender, RibbonControlEventArgs e )
        {
            whutSetStyle(tabCapStyle);
            p = new Word.ApplicationEvents4_WindowBeforeDoubleClickEventHandler(handler);
            wordApp.WindowBeforeDoubleClick += p;

        }

        private void handler(Word.Selection Sel, ref bool Cancel)
        {
            MessageBox.Show(Sel.Start.ToString());
            Cancel = false;

            wordApp.WindowBeforeDoubleClick -= p;
        }
    }
}
